package com.morningstar.system.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.morningstar.common.result.ServiceCheckResult;
import com.morningstar.system.dao.entity.SysMessageTemplate;
import com.morningstar.system.dao.mapper.SysMessageTemplateMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 消息模板服务类
 *
 * @author Admin
 */
@Slf4j
@Service
public class SysMessageTemplateService extends ServiceImpl<SysMessageTemplateMapper, SysMessageTemplate> {


    public SysMessageTemplate getByTemplateKey(String templateKey) {
        return this.getOne(this.lambdaQuery().eq(SysMessageTemplate::getTemplateKey, templateKey));
    }

    public ServiceCheckResult checkAddAndUpdate(SysMessageTemplate sysMessageTemplate) {
        LambdaQueryWrapper<SysMessageTemplate> queryWrapper = new LambdaQueryWrapper<SysMessageTemplate>()
                .eq(SysMessageTemplate::getTemplateKey, sysMessageTemplate.getTemplateKey())
                .ne(sysMessageTemplate.getTemplateId() != null, SysMessageTemplate::getTemplateId, sysMessageTemplate.getTemplateId());
        if (this.count(queryWrapper) > 0) {
            return new ServiceCheckResult(true, "消息模板KEY已存在");
        }
        return new ServiceCheckResult(false);
    }

}
